﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Windows.Forms;
using System.Drawing.Drawing2D;

namespace Grizzy_X
{
    static class Controller
    {
        public static CLayer CurrentLayer;
        public static int CurrentID;
        public static List<int> Selected;
        public static List<int> selectsimple;
        public static List<int> snapsameedge;
        public static string SelectedObjectType;
        public static Point selectnodeindex;
        public static Point selectlineindex;
        public static int CountForm = 0;
        public static int CountLayer = 0;
        public static int CountBase = 0;
        public static bool check = false;
        //新加了一个状态
        public enum OperationStyles {None, Selecting, Editing, Panning, Zoomin, Zoomout,Zoomtolayer,Dragging,Editnode,SplitLine,SplitPolygon};
        public static OperationStyles current_Operation;
        public static bool DBExists = false;//判断数据库是否存在
        public static string StartOperation;
        public static string mapname;
        public static string MapPath;
        public static CMap MyMap;   //实例化一个Map

       //记录窗口大小，在Viewer中使用
        public static int View_Height;
        public static int View_Width;
        public static Bitmap Blank;
        //给图例显示的公共变量
        public static List<CLegend> MapLegends = new List<CLegend>();

        public static int CurrentLegendIndex;
        public static int WaitForChangeLegendIndex = -1;

        public static string filename;
        public static void newProject()
        {

        }
        public static void Dispose()
        {
            MyMap = new CMap();
            CountBase = 0;
            CountForm = 0;
            CountLayer = 0;
            current_Operation = OperationStyles.None;
            MapLegends.Clear();
            WaitForChangeLegendIndex = -1;
        }
        public static PictureBox FindLayer(int id)
        {
            PictureBox TempF=new PictureBox();
            if(CountBase==0&&CountLayer==0)
                return null;
            if(CountBase==0&&CountLayer!=0)
            {
                for (int i = 1;i<=CountLayer; i++)
                {
                    if (i == 1)
                        TempF = (PictureBox)(MainForm.f1.panelbase.Controls.Find("Layer1", false))[0];
                    else
                        TempF = (PictureBox)(TempF.Controls.Find("Layer" + i.ToString(), false))[0]; 
                    if (i == CountLayer-Controller.MyMap.Layer_Order.IndexOf(id))
                        return TempF;        
                }
            }
            if (CountBase != 0 && CountLayer != 0)
            {
                for (int i =1; i <=CountBase; i++)
                {
                    if (i == 1)
                    {
                        TempF = (PictureBox)(MainForm.f1.panelbase.Controls.Find("Base1", false))[0];
                    }
                    else
                    {
                        TempF = (PictureBox)(TempF.Controls.Find("Base" + i.ToString(), false))[0];
                    }
                }
                for (int i = 1; i <= CountLayer; i++)
                {
                    if (i == 1)
                        TempF = (PictureBox)(TempF.Controls.Find("Layer1", false))[0];
                    else
                        TempF = (PictureBox)(TempF.Controls.Find("Layer" + i.ToString(), false))[0];
                    if (i == CountLayer - Controller.MyMap.Layer_Order.IndexOf(id))
                        return TempF; 
                }
            }
            return null;
        }
        public static PictureBox FindBase(int id)
        {
            PictureBox TempF = new PictureBox();
            if (CountBase == 0)
                return null;
            if (CountBase != 0)
            {
                for (int i = 1; i <= CountBase;i++ )
                {
                    if (i == 1)
                        TempF = (PictureBox)(MainForm.f1.panelbase.Controls.Find("Base1", false))[0];
                    else
                        TempF = (PictureBox)(TempF.Controls.Find("Base" + i.ToString(), false))[0];
                    if (i==CountBase-Controller.MyMap.Base_Order.IndexOf(id))
                        return TempF;
                }
            }
            return null;
        }
        public static bool IfPointInPolygon(List <PointF> Poly, PointF P)
        {
            double angle=0;
            Poly.Add(Poly[0]);
            for (int i = 0; i + 1 < Poly.Count(); i++)
            {
                double x1 = Poly[i].X - P.X;
                double y1 = Poly[i].Y - P.Y;
                double x2 = Poly[i + 1].X - P.X;
                double y2 = Poly[i + 1].Y - P.Y;
                angle+=Math.Acos((x1*x2+y1*y2)/(Math.Sqrt(x1*x1+y1*y1)*Math.Sqrt(x2*x2+y2*y2)));
            }
            Poly.RemoveAt(Poly.Count-1);
            if ((Math.Abs(angle - Math.PI * 2) < 0.01)||(angle > Math.PI * 2))
                return true;
            else
                return false;
        }
        //a-b, c-d
        public static PointF SegmentIntersect(PointF a, PointF b, PointF c, PointF d)
        {
            double k1;
            double k2;
            try
            {k1=(a.Y-b.Y)/(a.X-b.X);}
            catch
            {k1=999999;}
             try
            {k2=(c.Y-d.Y)/(c.X-d.X);}
            catch
            {k2=999999;}

            if(k1==k2)
            {
                return new PointF(-1,-1);
            }
            else
            {
                PointF intersection = new PointF();
                intersection.X = ((b.X - a.X) * (c.X - d.X) * (c.Y - a.Y) - c.X * (b.X - a.X) * (c.Y - d.Y) + a.X * (b.Y - a.Y) * (c.X - d.X)) / ((b.Y - a.Y) * (c.X - d.X) - (b.X - a.X) * (c.Y - d.Y));
                intersection.Y = ((b.Y - a.Y) * (c.Y - d.Y) * (c.X - a.X) - c.Y * (b.Y - a.Y) * (c.X - d.X) + a.Y * (b.X - a.X) * (c.Y - d.Y)) / ((b.X - a.X) * (c.Y - d.Y) - (b.Y - a.Y) * (c.X - d.X));
                if ((intersection.X - a.X) * (intersection.X - b.X) <= 0 && (intersection.X - c.X) * (intersection.X - d.X) <= 0 && (intersection.Y - a.Y) * (intersection.Y - b.Y) <= 0 && (intersection.Y - c.Y) * (intersection.Y - d.Y) <= 0)
                    return intersection;
                else
                    return new PointF(-1, -1);
            }
        }

        public static bool Draw_Layer()
        {
            try
            {
                PictureBox ZoomTemp = new PictureBox();
                for (int j = Controller.MyMap.Base_Order.Count - 1; j >= 0; j--)
                {
                    ZoomTemp = Controller.FindBase(Controller.MyMap.Base_Order[j]);
                    ZoomTemp.Image = Controller.MyMap.BaseMap_List[Controller.MyMap.Base_Order[j] - 1].DrawChange();
                    if (Controller.MyMap.BaseMap_List[Controller.MyMap.Base_Order[j] - 1].Visible == false)
                    {
                        ZoomTemp.Image = Controller.Blank;
                    }
                }
                for (int i = Controller.MyMap.Layer_Order.Count() - 1; i >= 0; i--)
                {
                    ZoomTemp = Controller.FindLayer(Controller.MyMap.Layer_Order[i]);
                    ZoomTemp.Image = null;
                    if (Controller.MyMap.Layer_List[Controller.MyMap.Layer_Order[i] - 1].Layer_Type == CLayer.LayerType.CPointLayer)
                    {
                        ZoomTemp.Image = ((CPointLayer)Controller.MyMap.Layer_List[Controller.MyMap.Layer_Order[i] - 1]).drawpoint();
                    }
                    else if (Controller.MyMap.Layer_List[Controller.MyMap.Layer_Order[i] - 1].Layer_Type == CLayer.LayerType.CLineLayer)
                    {
                        ZoomTemp.Image = ((CLineLayer)Controller.MyMap.Layer_List[Controller.MyMap.Layer_Order[i] - 1]).drawlinelayer();
                    }
                    else if (Controller.MyMap.Layer_List[Controller.MyMap.Layer_Order[i] - 1].Layer_Type == CLayer.LayerType.CPolygonLayer)
                    {
                        ZoomTemp.Image = ((CPolygonLayer)Controller.MyMap.Layer_List[Controller.MyMap.Layer_Order[i] - 1]).drawpolygon();
                    }
                    else if (Controller.MyMap.Layer_List[Controller.MyMap.Layer_Order[i] - 1].Layer_Type == CLayer.LayerType.CTextLayer)
                    {
                        ZoomTemp.Image = ((CTextLayer)Controller.MyMap.Layer_List[Controller.MyMap.Layer_Order[i] - 1]).drawtext();
                    }
                    if (Controller.MyMap.Layer_List[Controller.MyMap.Layer_Order[i] - 1].bVisible == false)
                   { 
                        ZoomTemp.Image = Controller.Blank;
                    }
                }
                return true;
            }
            catch
            {
                return false;
            }
        }
    }
}
